
#include "test_code.h"
#include "base_util.h"
#include "codec_config.h"
#include "message_queue.h"
#include <cstdint>
#include <cstdio>
#include "operate_define.h"

void TestHwDecodeVideoData(const std::string & filename)
{
	InputFormatContext input_output_context;
	CodecConfig codec_config;

	int result = input_output_context.OpenUrlWithFindStreamInfo(filename);
	if (result)
	{
		codec_config.m_video_config.m_hw_name = "qsv";
		result = input_output_context.CreateVideoDecoderCtx("", codec_config);
		if (result)
		{
            int64_t frame_count = 0;
			FrameMessageQueue packet_frame_operate;
			packet_frame_operate.InitMessageQueue(1024);
			PrintLogMsg("create video decoder ctx success");
			ReadPacketDataWithCallback(&input_output_context, nullptr, [&packet_frame_operate,&frame_count](InputFormatContext * input_output_context, AVPacket * packet){

				if (packet->stream_index == input_output_context->VideoMediaStream()->AvStream()->index)
					DecodePacketDataToMessageQueue(input_output_context->VideoDecoder(), packet, packet_frame_operate);
				ProcessFrameDataFromMessageQueue(packet_frame_operate, [&frame_count](AVFrame * frame){

					// static int64_t frame_count = 0;
					printf("new frame count : %ld and format : %d.\n", frame_count++, frame->format);

				}, false);
				return true;
			});
			DecodePacketDataToMessageQueue(input_output_context.VideoDecoder(), nullptr, packet_frame_operate);
			ProcessFrameDataFromMessageQueue(packet_frame_operate, [&frame_count](AVFrame * frame){

                printf("new frame count : %ld and format : %d.\n", frame_count++, frame->format);

			}, false);
		}
		else
		{
			PrintLogMsg("create video decoder ctx failed");
		}
	}
	else
	{
		PrintLogMsg("open file failed");
	}
}